package top.huhuiyu.spirngboot2.total.service.message.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.huhuiyu.spirngboot2.total.base.BasePageResult;
import top.huhuiyu.spirngboot2.total.base.BaseResult;
import top.huhuiyu.spirngboot2.total.base.PageBean;
import top.huhuiyu.spirngboot2.total.dao.message.TbMessageDAO;
import top.huhuiyu.spirngboot2.total.dao.message.TbMessageReplyDAO;
import top.huhuiyu.spirngboot2.total.entity.AuthInfo;
import top.huhuiyu.spirngboot2.total.entity.message.TbMessage;
import top.huhuiyu.spirngboot2.total.entity.message.TbMessageReply;
import top.huhuiyu.spirngboot2.total.service.AuthService;
import top.huhuiyu.spirngboot2.total.service.message.TbMessageService;
import top.huhuiyu.spirngboot2.total.utils.AuthInfoUtils;
import top.huhuiyu.spirngboot2.total.vo.UserInfo;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbMessageServiceImpl implements TbMessageService {
    private final TbMessageDAO tbMessageDAO;
    private final AuthService authService;
    private final TbMessageReplyDAO tbMessageReplyDAO;

    @Override
    public BaseResult add(TbMessage tbMessage) {
        // 发帖人为当前登录用户
        AuthInfo authInfo = AuthInfoUtils.authInfo();
        tbMessage.setUid(authInfo.getUserId());
        // 添加
        int r = tbMessageDAO.insert(tbMessage);
        return r == 1 ? BaseResult.ok("发帖成功") : BaseResult.fail("发帖失败");
    }

    @Override
    public BasePageResult<TbMessage> queryByKey(Integer mid, PageBean pageBean) {
        TbMessage tbMessage = tbMessageDAO.selectById(mid);
        if (tbMessage == null) {
            return BasePageResult.failPage("查无数据");
        }
        QueryWrapper<TbMessageReply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid", mid);
        IPage<TbMessageReply> page = pageBean.toPage(TbMessageReply.class);
        tbMessageReplyDAO.selectPage(page, queryWrapper);
        pageBean.buildPageInfo(page);
        tbMessage.setReplies(page.getRecords());
        return BasePageResult.ok(pageBean, tbMessage);

    }

    @Override
    public BasePageResult<List<TbMessage>> query(PageBean pageBean, TbMessage tbMessage, Integer orderBy) {
        QueryWrapper<TbMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mid", "title", "uid", "praise", "hits", "reply", "lastupdate");
        queryWrapper.like(StringUtils.hasText(tbMessage.getTitle()), "title", tbMessage.getTitle());
        queryWrapper.eq(tbMessage.getUid() != null, "uid", tbMessage.getUid());
        if (orderBy == 1) {
            queryWrapper.orderByDesc("mid");
        } else if (orderBy == 2) {
            queryWrapper.orderByAsc("hits");
        } else if (orderBy == 3) {
            queryWrapper.orderByDesc("hits");
        }
        IPage<TbMessage> page = pageBean.toPage(TbMessage.class);
        tbMessageDAO.selectPage(page, queryWrapper);
        page.getRecords().forEach(message -> {
            UserInfo userInfo = authService.queryById(message.getUid());
            message.setUserInfo(userInfo.queryViewData());
        });

        pageBean.buildPageInfo(page);
        return BasePageResult.ok(pageBean, page.getRecords());
    }

}
